'use strict';
module.exports = {
    /**
     * XXXnameXXX
     * @url client/timedTask/pub/autoCancelOrder 前端调用的url参数地址
     * data 请求参数
     * @param {String} params1  参数1
     */
    main: async (event) => {
        let {
            data = {}, userInfo, util, filterResponse, originalParam
        } = event;
        let {
            customUtil,
            uniID,
            config,
            pubFun,
            vk,
            db,
            _
        } = util;
        let {
            uid
        } = data;
        let res = {
            code: 0,
            msg: ""
        };
        await vk.baseDao.del({
            dbName: "bidding-session",
            whereJson: {
                _id: _.exists(true),
            }
        });
        let places = await vk.baseDao.select({
            dbName: "bidding-places", // 表名
            getCount: false, 
            getMain: true, 
            pageIndex: 1, 
            pageSize: -1,
            whereJson: { 
                is_on: true,
                dateArr: _.neq(null),
                bingtimes: _.neq(null)
            }
        });
        let addpush = [];
        for (var i = 0; i < places.length; i++) {
            let place = places[i];
            let days = listDates(place.dateArr);
            if (days != undefined && days.length > 0) {
                for (var j = 0; j < days.length; j++) {
                    for (var k = 0; k < place.bingtimes.length; k++) {

                        if (place.bingtimes[k] != undefined) {
                            let no = `${place._id}${days[j]}${place.bingtimes[k].code[0]}${place.bingtimes[k].code[1]}`;
                            let find = await vk.baseDao.findByWhereJson({
                                dbName: "bidding-session",
                                whereJson: {
                                    no: no
                                }
                            });

                            if (vk.pubfn.isNull(find)) {
                                let start = new Date(`${days[j]} ${place.bingtimes[k].code[0]}:00+08:00`).getTime()

                                let addobj = {
                                    place_id: place._id,
                                    no: `${place._id}${days[j]}${place.bingtimes[k].code[0]}${place.bingtimes[k].code[1]}`,
                                    place_name: place.name,
                                    status: 0,
                                    start: start,
                                    start_str: `${days[j]} ${place.bingtimes[k].code[0]}`,
                                    end: new Date(`${days[j]} ${place.bingtimes[k].code[1]}:00+08:00`).getTime(),
                                    end_str: `${days[j]} ${place.bingtimes[k].code[1]}`,
                                }
                                addpush.push(addobj)
                            }
                        }
                    }
                }
            }
        }
        let addids = ''
        if (addpush.length > 0) {
            addids = await vk.baseDao.adds({
                dbName: "bidding-session", // 表名
                dataJson: addpush
            });
        }

        function listDates(array) {
            // 定义一个空数组，用于存放结果
            let result = [];
            // 使用Date对象，将数组中的日期字符串转换为日期对象
            let startDate = new Date(array[0]);
            let endDate = new Date(array[1]);
            // 使用一个循环，从开始日期到结束日期，每次增加一天
            for (let date = startDate; date <= endDate; date.setDate(date.getDate() + 1)) {
                // 将每个日期对象转换为日期字符串，格式为yyyy-mm-dd
                let dateString = date.toISOString().slice(0, 10);
                // 将日期字符串添加到结果数组中
                result.push(dateString);
            }
            // 返回结果数组
            return result;
        }

        // 业务逻辑结束-----------------------------------------------------------
        return {
            code: 0,
            msg: addids
        }
    }
}